package com.daydayup.mine1;

import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class Minesweeper {

	public static String[][] squares = null;

	@SuppressWarnings("resource")
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		System.out.println("扫雷游戏开始。。。。。");
		System.out.println("请输入行数：");
		int rows = scanner.nextInt();
		System.out.println("请输入列数：");
		int columns = scanner.nextInt();
		System.out.println("请输入雷的数量：");
		int count = scanner.nextInt();
		System.out.println("请输入第一个的横坐标：");
		int firstRow = scanner.nextInt();
		System.out.println("请输入第一个的纵坐标：");
		int firstColumn = scanner.nextInt();

		Set<Mine> mines = new HashSet<Mine>();
		addMineSet(firstRow, firstColumn, mines);

		Minesweeper minesweeper = new Minesweeper();
		// 初始化
		minesweeper.init(rows, columns);// 将开始的所有区方块生成^符号，好看

		minesweeper.generateMine(rows, columns, count, firstRow, firstColumn);// 生成雷
		while (mines.size() != rows * columns - count) {
			// 计算某个点周围雷的数量
			int mineSum = minesweeper.mineSum(firstRow, firstColumn, rows, columns);
			squares[firstRow - 1][firstColumn - 1] = mineSum + "";
			// 遍历二维数组
			minesweeper.print(squares, rows, columns);
			System.out.println("请输入下一个的横坐标：");
			int nextRow = scanner.nextInt();
			System.out.println("请输入下一个的纵坐标：");
			int nextColumn = scanner.nextInt();
			if (nextRow >= 0 && nextColumn >= 0 && nextRow < rows && nextColumn < columns) {
				if ("*".equals(squares[nextRow - 1][nextColumn - 1])) {
					System.out.println("踩到雷了，游戏结束！");
					return;
				}
			}
			firstRow = nextRow;
			firstColumn = nextColumn;
			addMineSet(firstRow, firstColumn, mines);
		}
		System.out.println("游戏成功！");
		scanner.close();
	}

	private static void addMineSet(int firstRow, int firstColumn, Set<Mine> mines) {
		Mine mine = new Mine();
		mine.setRow(firstRow);
		mine.setColumn(firstColumn);
		mines.add(mine);
	}

	/**
	 * 初始化
	 * 
	 * @param rows
	 * @param columns
	 */
	private void init(int rows, int columns) {
		squares = new String[rows][columns];
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				squares[i][j] = "^";
			}
			System.out.println();
		}
	}

	private void print(String[][] squares, int rows, int columns) {
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				System.out.print(squares[i][j] + "  ");
			}
			System.out.println();
		}

	}

	// 计算某个点周围雷的数量
	private int mineSum(int firstRow, int firstColumn, int rows, int columns) {
		int sum = 0;
		if (firstRow - 1 >= 1) {// 上一行存在
			sum = sumOfRow(firstRow, firstColumn, columns, sum, firstRow - 2);
		}
		if (true) {// 本行
			sum = sumOfRow(firstRow, firstColumn, columns, sum, firstRow - 1);
		}

		if (firstRow + 1 <= rows) {// 下一行存在
			sum = sumOfRow(firstRow, firstColumn, columns, sum, firstRow);
		}
		return sum;

	}

	/**
	 * 计算某个点所涉及的行的雷的个数
	 * 
	 * @param firstRow
	 * @param firstColumn
	 * @param columns
	 * @param sum
	 *            和
	 * @param stationRow
	 *            具体是哪一行
	 * @return
	 */
	private int sumOfRow(int firstRow, int firstColumn, int columns, int sum, int stationRow) {
		int tempColumn = firstColumn - 1;
		for (int i = 0; i < 3; i++) {
			if (tempColumn >= 1 && tempColumn <= columns) {
				if ("*".equals(squares[stationRow][tempColumn - 1])) {
					sum += 1;
				}
			}
			tempColumn += 1;
		}
		return sum;
	}

	/**
	 * 生成雷
	 * 
	 * @param rows
	 *            总行数
	 * @param columns
	 *            总列数
	 * @param count
	 *            雷的数量
	 * @param firstRow
	 *            输入的第一个横坐标
	 * @param firstColumn
	 *            输入的第一个纵坐标
	 */
	private void generateMine(int rows, int columns, int count, int firstRow, int firstColumn) {
		Set<Mine> mines = creatMineSet(rows, columns, count, firstRow, firstColumn);
		for (Mine mine : mines) {
			squares[mine.getRow() - 1][mine.getColumn() - 1] = "*";
		}
	}

	/**
	 * 生成存放雷的随机数的Set
	 * 
	 * @param rows
	 *            总行数
	 * @param columns
	 *            总列数
	 * @param count
	 *            雷的数量
	 * @param firstRow
	 *            输入的第一个横坐标
	 * @param firstColumn
	 *            输入的第一个纵坐标
	 * @return Set集合
	 */
	private Set<Mine> creatMineSet(int rows, int columns, int count, int firstRow, int firstColumn) {
		Set<Mine> mines = new HashSet<>();
		Random random = new Random();
		// 生成Set
		while (mines.size() != count) {
			Mine mine = new Mine();
			int mineRow = random.nextInt(rows) + 1;
			int mineColumn = random.nextInt(columns) + 1;

			if (mineRow == firstRow && mineColumn == firstColumn) {// 如果生成的随机数为第一个横纵坐标
				continue;
			}

			mine.setRow(mineRow);
			mine.setColumn(mineColumn);
			mines.add(mine);
		}

		return mines;
	}

}
